package com.charging.business.media;


import com.charging.business.utils.ToolsUtil;

/**
 * 双片式OBU车辆信息文件(DF01/EF01)的编码信息
 * 79bytes
 *
 * @author lzj
 */
public class ObuEf01EdModel {
    public static final int MSG_LEN = 79;

    /**
     * 车牌号码(01-12,12,an)
     */
    public byte[] plateNo = new byte[12];

    /**
     * 车牌颜色(13-14,2,n)高字节： 00H	低字节：0x00 –蓝色； 0x01–黄色； 0x02 –黑色； 0x03 –白色； 0x04－渐变绿色； 0x05－黄绿双拼色； 0x06－蓝白渐变；0x07～ 0xFF 保留
     */
    public byte[] plateColor = new byte[2];

    /**
     * 车型(15-15,1,n)0~4:客1~客9；11~16：货1~货6；21~26：专1~专6
     */
    public byte[] vehicleType = new byte[1];

    /**
     * 用户类型(16-16,1,n)
     * 0-普通车； 6-公务车； 8-军警车； 10-紧急车； 12-免费；14-车队； 0～20 内其他：自定义；
     * 21-绿通车； 22-联合收割机 ; 23-抢险救灾; 24－J1专用集装箱车；25-大件运输; 26－应急救援车；27－货车列车或半挂汽车列车;28-J2集装箱车
     */
    public byte[] vehicleUserType = new byte[1];

    /**
     * 车辆扩展信息(17-63,26,客货不同)
     */
    public byte[] extension = new byte[63];

    public static ObuEf01EdModel parseSelf(byte[] buff) {
        int index = 0;
        ObuEf01EdModel ret = new ObuEf01EdModel();
        index += ToolsUtil.arrcopy(buff, index, ret.plateNo);
        index += ToolsUtil.arrcopy(buff, index, ret.plateColor);
        index += ToolsUtil.arrcopy(buff, index, ret.vehicleType);
        index += ToolsUtil.arrcopy(buff, index, ret.vehicleUserType);
        index += ToolsUtil.arrcopy(buff, index, ret.extension);

        // GBK车牌号码矫正
        chkVPlate(ret.plateNo);

        return ret;
    }

    /**
     * 预解析
     *
     * @param strHex
     * @return
     */
    public static ObuEf01EdModel parse(String strHex) {
        if (strHex == null || strHex.isEmpty()) {
            return null;
        }
//        if (strHex.length() < DOBUDFEF01ED.MSG_LEN * 2) {
//            return null;
//        }
        if (!ToolsUtil.chkHexChar(strHex)) {
            return null;
        }
        byte[] buff = ToolsUtil.hex2bin(strHex);
        return parseSelf(buff);
    }

    private static void chkVPlate(byte[] zb) {
        for (int aa = zb.length - 1; aa >= 0; aa--) {
            if (zb[aa] == (byte) 0xff) {
                zb[aa] = 0;
            } else {
                break;
            }
        }
    }

    /**
     * 信息解码
     *
     * @return
     */
    public ObuEf01Model decoded() {
        ObuEf01Model dd = new ObuEf01Model();
        dd.plateNo = ToolsUtil.bin2str(plateNo);
        dd.plateColor = ToolsUtil.bin2int(plateColor);
        dd.vehicleType = ToolsUtil.bin2int(vehicleType);
        dd.vehicleUserType = ToolsUtil.bin2int(vehicleUserType);
        // 客车
        if (dd.vehicleType < 10) {
            dd.vehicleLength = ToolsUtil.bin2int(extension, 0, 2) * 100;
            dd.vehicleWidth = ToolsUtil.bin2int(extension, 2, 1) * 100;
            dd.vehicleWeight = ToolsUtil.bin2int(extension, 3, 1) * 100;
            dd.vehicleWheel = ToolsUtil.bin2int(extension, 4, 1);
            dd.vehicleAxles = ToolsUtil.bin2int(extension, 5, 1);
            dd.vehicleWheelBase = ToolsUtil.bin2int(extension, 6, 2) * 100;
            dd.vehicleSeat = ToolsUtil.bin2int(extension, 8, 3);
            dd.specDesc = ToolsUtil.bin2hex(extension, 11, 16);
            dd.vehicleVin = ToolsUtil.bin2hex(extension, 27, 16);
            dd.spare = ToolsUtil.bin2hex(extension, 43, 20);
        } else {
            // 货车
            dd.vehicleLength = ToolsUtil.bin2int(extension, 0, 2);
            dd.vehicleWidth = ToolsUtil.bin2int(extension, 2, 2);
            dd.vehicleWeight = ToolsUtil.bin2int(extension, 4, 2);
            dd.approvedLoad = ToolsUtil.bin2int(extension, 6, 3);
            dd.unladenMass = ToolsUtil.bin2int(extension, 9, 3);
            dd.grossMass = ToolsUtil.bin2int(extension, 12, 3);
            dd.vehicleSeat = ToolsUtil.bin2int(extension, 15, 1);
            dd.vehicleVin = ToolsUtil.bin2hex(extension, 16, 17);
            dd.specDesc = ToolsUtil.bin2hex(extension, 33, 16);
            dd.spare = ToolsUtil.bin2hex(extension, 49, 14);
        }

        //不为普通车,J1车,应急救援,挂车,J2车
        if (dd.vehicleUserType != 0x00 && dd.vehicleUserType != 0x08 && dd.vehicleUserType != 0x18 && dd.vehicleUserType != 0x1A && dd.vehicleUserType != 0x1B && dd.vehicleUserType != 0x1C) {
            //强制转换为普通ETC车辆
            dd.vehicleUserType = 0x00;
        }

        return dd;
    }
}
